import SCons
import os
from distutils import sysconfig

import builders

# Setup command line options
def ToList_Validate(key, val, env):
    pass

def ToList_Convert(val):
    return val.split(',')

def ToList(key, help, default):
    return (key, help, default, ToList_Validate, ToList_Convert)

opts = Options('.sconfig')
opts.AddOptions(
    ('VARIANT', 'Debug or Release', 'release'),
    ('CXX', 'C++ Compiler to use'),
    ToList('TOOLS', 'Toolset to use', 'default'),
    ('CXXFLAGS', 'Extra flags for the C++ compiler'),
    ('HACK1', 'Force use of release boost.python library', ''),

    PathOption('BOOST_PATH',
        'Path to Boost installation',
        '',
        PathOption.PathIsDir ),

    PathOption('PYTHON_PATH',
        'Path to Python installation',
        sysconfig.PREFIX,
        PathOption.PathIsDir ),
    ('BOOST_PYTHON_LIB_BASE',
        'Name of boost_python library',
        'boost_python-mt' ),
)

env = Environment( options = opts) #, tools = ['default','icl'] )

builders.install(env)

# There seems to be some problem with relative paths unless we force
#  them into Dir nodes here
env['BOOST_PATH'] = Dir( env['BOOST_PATH'] )
env['PYTHON_PATH'] = Dir( env['PYTHON_PATH'] )

if env['VARIANT'] == 'debug' and not env['HACK1']:
    env['BOOST_PYTHON_LIB_NAME'] = '$BOOST_PYTHON_LIB_BASE-gyd'
else:
    env['BOOST_PYTHON_LIB_NAME'] = '$BOOST_PYTHON_LIB_BASE'


def PYDSuffix( target, source, env, for_signature ):
    return ( env['VARIANT'] == 'debug' and '_d' or '' ) + sysconfig.get_config_var('SO')

    

env.Append(
#    CXXFLAGS = ['/EHsc','/GR'],
    CPPPATH = [
        '$BOOST_PATH/include',
        sysconfig.get_python_inc( prefix = env.subst('$PYTHON_PATH') ),
    ],
    LIBPATH = [
        '$BOOST_PATH/lib',
        sysconfig.PREFIX+'/libs',
    ],
    PYDLIBSUFFIX = PYDSuffix,
    BOOST_PYTHON_LIB = env.subst('$BOOST_PATH/lib/$LIBPREFIX$BOOST_PYTHON_LIB_NAME$LIBSUFFIX'),
    BOOST_PYTHON_DLL = env.subst('$BOOST_PATH/lib/$SHLIBPREFIX$BOOST_PYTHON_LIB_NAME$SHLIBSUFFIX'),
    MAKEWRAPPER = File("#/scripts/MakeWrapper.py"),
)


env['PDB'] = 'pyxx.pdb'

parser = SConscript( 'parser/SConscript', 
    exports = 'env',
    build_dir = env.subst('$CXX-$VARIANT'),
    src_dir = '.',
    duplicate = 0 )

pyxx_helper = SConscript( 'src/SConscript',
    exports = 'env',
    build_dir = env.subst('$CXX-$VARIANT'),
    src_dir = '.',
    duplicate = 0 )


pythonpath = str(env['PYTHON_PATH'])
libpath = sysconfig.get_python_lib(True, prefix=pythonpath)
pyxxlibpath = os.path.join( libpath, 'pyxx' )
includepath = sysconfig.get_python_inc(True, prefix=pythonpath)
pyxxincludepath = os.path.join( includepath, 'pyxx' )
scriptspath = os.path.join(str(env['PYTHON_PATH']), 'scripts')



installers =[
    env.Install(pyxxlibpath, parser),
    env.Install(libpath, pyxx_helper),
    env.Install(pyxxlibpath, env.Glob('pyxx/*.py')),
    env.Install(pyxxincludepath, env.Glob('include/pyxx/*.h')),
    env.Install(pyxxlibpath, "$BOOST_PYTHON_DLL") ,
    env.Install(libpath, "$BOOST_PYTHON_DLL"),
    env.Install(scriptspath, env.Glob('scripts/*.py')),
    env.Install(pyxxlibpath, "$BOOST_PYTHON_DLL") ]
env.Alias( 'install', installers)

mylibpath = env.subst('$CXX-$VARIANT/lib')
pyxxmylibpath = os.path.join(mylibpath, 'pyxx')

env.Alias( 'install_for_tests', [
    env.Install(pyxxmylibpath, parser),
    env.Install(mylibpath, pyxx_helper),
    env.Install(pyxxmylibpath, env.Glob('pyxx/*.py')),
    env.Install(pyxxmylibpath, "$BOOST_PYTHON_DLL"),
    env.Install(mylibpath, "$BOOST_PYTHON_DLL") 
    ])

tests = SConscript( 'tests/SConscript',
    exports = 'env',
    build_dir = env.subst('$CXX-$VARIANT'),
    src_dir = '.',
    duplicate = 0 )

env.Depends(installers, tests) # do not install until the tests have been run



